﻿Imports System.IO
Imports System.Text

Module ConfigFile

    Private settingsFile As String = AppPath & "\setting.sfc"
    Private settingsData As List(Of String) = New List(Of String)
    Private settingSave As Boolean = False

    Friend ddrawIni As List(Of String) = New List(Of String) ' содержимое ddraw.ini
    Friend sfallIni As List(Of String) = New List(Of String) ' содержимое sfall.ini или пользовательского ini файла
    Friend f2ResIni As String()                              ' содержимое f2_res.ini
    Friend sfModsIni As String()                             ' содержимое sfall-mods.ini

    Friend sfallFile As String = "\sfall.ini"                ' текущий пользовательский ini файл
    Friend sfallModsFile As String = "\sfall-mods.ini"
    Friend cfgDefaultFile As String = "\fallout2.cfg"

    Friend cfgGameData As List(Of String) = New List(Of String)
    Friend cfgSave As Boolean = False
    'Friend useUserIni As Boolean = True

    Private Function getSfallModsFile() As Boolean
        Dim iniPath = ConfigFuncs.GetConfigIniValue("IniConfigFolder")

        If iniPath <> Nothing Then
            iniPath = iniPath.Replace("/", "").Trim()
            Dim modsFile = "\" & iniPath.Trim("\") & sfallModsFile

            If File.Exists(AppPath & modsFile) Then
                sfallModsFile = modsFile
                Return True
            End If
        End If

        Return File.Exists(AppPath & sfallModsFile)
    End Function

    Friend Sub MainInitialization()
        ReadSettingsFile()

        ddrawIni = File.ReadAllLines(AppPath & "\ddraw.ini", Encoding.Default).ToList

        ' sfall.ini
        Dim cfgFile = GetSfallConfigFile()
        If (cfgFile IsNot Nothing) Then
            sfallIni = File.ReadAllLines(AppPath & cfgFile, Encoding.Default).ToList
        End If

        If File.Exists(AppPath & "\f2_res.ini") Then
            f2ResIni = File.ReadAllLines(AppPath & "\f2_res.ini", Encoding.Default)
        End If

        ' sfall-mods.ini
        If getSfallModsFile() Then
            sfModsIni = File.ReadAllLines(AppPath & sfallModsFile, Encoding.Default)
        End If

        GetGameCFGFile()

        If File.Exists(AppPath & cfgDefaultFile) Then
            cfgGameData = File.ReadAllLines(AppPath & cfgDefaultFile, Encoding.Default).ToList
        End If
    End Sub

    Private Sub ReadSettingsFile()
        If File.Exists(settingsFile) Then
            settingsData = File.ReadAllLines(settingsFile, Encoding.Default).ToList
        Else
            settingsData.Add("[Main]")
            settingsData.Add("game_exe=")
            settingsData.Add("custom_ini=")
            settingsData.Add("game_cfg=")
        End If
    End Sub

    ''' <summary>
    ''' Получает имя текущего пользовательского ini файл
    ''' </summary>
    Private Function GetSfallConfigFile() As String
        Dim customIni = ConfigFuncs.GetIniValue("[Main]", "custom_ini", settingsData.ToArray())
        If (customIni IsNot Nothing AndAlso File.Exists(AppPath & customIni)) Then
            sfallFile = customIni ' заменяем
        ElseIf File.Exists(AppPath & sfallFile) = False Then
            sfallFile = Nothing
            'useUserIni = False
        End If
        Return sfallFile
    End Function

    Friend Sub SetCustomConfigFile(iniPath As String)
        sfallFile = "\" + Path.GetFileName(iniPath)
        sfallIni = File.ReadAllLines(AppPath & sfallFile, Encoding.Default).ToList

        ConfigFuncs.SetIniValue("[Main]", "custom_ini", sfallFile, settingsData)
        settingSave = True
    End Sub

    Friend Sub MakeSfallIni()
        sfallFile = "\sfall.ini"
        sfallIni = ddrawIni.ToList()
    End Sub

    Friend Function GetGameExe() As String
        Dim customExe = ConfigFuncs.GetIniValue("[Main]", "game_exe", settingsData.ToArray())
        If (customExe Is Nothing OrElse customExe = String.Empty) Then Return "FALLOUT2.EXE"

        Return customExe
    End Function

    Friend Sub SetGameExe(ByVal exe As String)
        ConfigFuncs.SetIniValue("[Main]", "game_exe", exe, settingsData)
        settingSave = True
    End Sub

    Private Sub GetGameCFGFile()
        Dim cfg = ConfigFuncs.GetConfigIniValue("ConfigFile")
        If (String.IsNullOrEmpty(cfg) = False) Then
            cfgDefaultFile = "\" & cfg
        Else
            cfg = ConfigFuncs.GetIniValue("[Main]", "game_cfg", settingsData.ToArray())
            If (String.IsNullOrEmpty(cfg) = False) Then cfgDefaultFile = cfg ' заменяем
        End If
    End Sub

    Friend Sub SetGameCFGFile(ByVal cfg As String)
        cfg = "\" & cfg
        If (String.Compare(cfgDefaultFile, cfg, True) = 0) Then Return

        cfgDefaultFile = cfg ' заменяем
        ConfigFuncs.SetIniValue("[Main]", "game_cfg", cfgDefaultFile, settingsData)

        settingSave = True
    End Sub

    Friend Sub SaveAllConfigFiles()
        Mods.SaveModsList()

        If ddrawIni.Count Then File.WriteAllLines(AppPath & "\ddraw.ini", ddrawIni.ToArray, Encoding.Default)

        If sfallFile IsNot Nothing AndAlso ddrawIni.Count Then File.WriteAllLines(AppPath & sfallFile, sfallIni.ToArray, Encoding.Default)

        If f2ResIni IsNot Nothing Then File.WriteAllLines(AppPath & "\f2_res.ini", f2ResIni, Encoding.Default)

        If sfModsIni IsNot Nothing Then File.WriteAllLines(AppPath & sfallModsFile, sfModsIni, Encoding.Default)

        If cfgSave Then File.WriteAllLines(AppPath & cfgDefaultFile, cfgGameData.ToArray, Encoding.Default)

        If settingSave Then File.WriteAllLines(settingsFile, settingsData.ToArray, Encoding.Default)

        cfgSave = False
        settingSave = False
    End Sub

    Friend Sub OpenConfig(ByRef config As String)
        Dim isExit As Boolean = True

        SaveAllConfigFiles()
        Try
            Process.Start(AppPath & config)
        Catch
            Try
                Process.Start("Notepad.exe", AppPath & config)
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Ошибка открытия")
                isExit = False
            End Try
        End Try

        If (isExit) Then Application.Exit()
    End Sub

End Module
